package cn.glutnn.subject.domain.service.impl;

import cn.glutnn.subject.common.enums.IsDeletedFlagEnum;
import cn.glutnn.subject.domain.convert.SubjectCategoryConverter;
import cn.glutnn.subject.domain.convert.SubjectLabelConverter;
import cn.glutnn.subject.domain.enyity.SubjectLabelBO;
import cn.glutnn.subject.domain.service.SubjectLabelDomainService;
import cn.glutnn.subject.infra.basic.entity.SubjectCategory;
import cn.glutnn.subject.infra.basic.entity.SubjectLabel;
import cn.glutnn.subject.infra.basic.entity.SubjectMapping;
import cn.glutnn.subject.infra.basic.service.SubjectCategoryService;
import cn.glutnn.subject.infra.basic.service.SubjectLabelService;
import cn.glutnn.subject.infra.basic.service.SubjectMappingService;
import com.baomidou.mybatisplus.extension.service.IService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * (SubjectCategory)表服务接口
 *
 * @author msh
 * @since 2024-11-20 15:42:42
 */

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;

    @Override
    public void addLabel(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl addLabel subjectLabelBO:{}", subjectLabelBO);
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabelService.save(subjectLabel);
    }

    @Override
    public void update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl update subjectLabelBO:{}", subjectLabelBO);
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabelService.updateById(subjectLabel);
    }

    @Override
    public void removeById(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl removeById subjectLabelBO:{}", subjectLabelBO);
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabelService.removeById(subjectLabel.getId());
    }

    @Override
    public List<SubjectLabelBO> queryAllLabel() {
        return SubjectLabelConverter.INSTANCE
                .convertLabelListToBoList(subjectLabelService.lambdaQuery()
                        .eq(SubjectLabel::getIsDeleted, IsDeletedFlagEnum.UN_DELETED).list());
    }

    @Override
    public List<SubjectLabelBO> queryLabelBySubjectId(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl queryLabelBySubjectId subjectLabelBO:{}", subjectLabelBO);
        }
        List<SubjectMapping> subjectMappingList = subjectMappingService.lambdaQuery()
                .eq(SubjectMapping::getCategoryId, subjectLabelBO.getCategoryId()).list();
        List<Long> ids = subjectMappingList.stream().map(SubjectMapping::getLabelId).toList();
        if (ids.isEmpty()) {
            return null;
        }
        return SubjectLabelConverter.INSTANCE.convertLabelListToBoList(subjectLabelService.lambdaQuery()
                .eq(SubjectLabel::getIsDeleted, IsDeletedFlagEnum.UN_DELETED.getCode())
                .in(SubjectLabel::getId, ids)
                .list());
    }
}

